// 高亮当前导航项
function highlightActiveNav() {
  const path = window.location.pathname.split('/').pop() || 'index.html';

  document.querySelectorAll('.nav-item').forEach(item => {
    item.classList.remove('active');
  });

  const pageMap = {
    'index.html': 'navHome',
    'mall.html': 'navMall',
    'points.html': 'navPoints',
    'profile.html': 'navProfile'
  };

  const activeId = pageMap[path];
  if (activeId) {
    document.getElementById(activeId)?.classList.add('active');
  }
}

// 加载组件
function loadComponent(componentName, targetElementId) {
  return fetch(`components/${componentName}.html`)
    .then(res => res.text())
    .then(html => {
      document.getElementById(targetElementId).innerHTML = html;
    });
}

// 页面初始化
document.addEventListener('DOMContentLoaded', () => {
  loadComponent('navbar', 'navbar-container')
    .then(() => {
      highlightActiveNav();
    });
});
